﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.CoursePlan
{
    public partial class CourseOutLineModify : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            strPageUserType.Add("mana");
            strPagePrivilege.Add("KCXYM");
            if (Request.QueryString["ID"] != null)
            {
                if (!IsPostBack)
                {
                    int ID;
                    if (int.TryParse(Request.QueryString["ID"].ToString(), out ID))
                    {
                        CouOutLinID.Value = ID.ToString();
                        DataBinds(ID);
                        tb_Submitter.Attributes.Add("readOnly", "-1");
                        tb_Time.Attributes.Add("readOnly", "-1");
                        tb_CourseName.Attributes.Add("readOnly", "-1");
                        tb_CourseCode.Attributes.Add("readOnly", "-1");
                        
                    }
                    else
                    {
                        ERRLbl.Text = "系统异常，参数不合法！";
                        ERRLbl.Visible = true;
                        ModifyBtn.Visible = false;
                    }
                }
            }
            else
            {
                ERRLbl.Text = "系统异常，参数为空！";
                ERRLbl.Visible = true;
                ModifyBtn.Visible = false;
            }
        }

        protected void DataBinds(int ID)
        {
            LabMS.BLL.CourseOutLine couOutLin = new LabMS.BLL.CourseOutLine();
            LabMS.Model.CourseOutLine couOutLinModel = new LabMS.Model.CourseOutLine();

            couOutLinModel = couOutLin.GetModel(ID);
            try
            {
                tb_Submitter.Text = couOutLinModel.COutLine_Submiter;
                tb_Time.Text = string.Format("{0:d}", couOutLinModel.COutLine_SubmitTime);
                tb_CourseName.Text = couOutLinModel.COutLine_Course;
                tb_CourseCode.Text = couOutLinModel.COutLine_CourseCode;
                tb_KeyWord.Text = couOutLinModel.COutLine_KeyWord;
                tb_Content.Text = couOutLinModel.COutLine_Content.Replace("\r\n", "<br/>");
                tb_CourseID.Text = couOutLinModel.COutLine_CourseID.ToString();
                AttachID.Value = couOutLinModel.COutLine_Attach.ToString();
                if (couOutLinModel.COutLine_Attach == null)
                {
                    a1.Visible = false;
                    AttachmentName.Visible = false;
                    IsDelCB.Checked = true;
                    fp_Attachment.Attributes.Add("style", "display:block");
                }
                else
                {
                    AttachmentName.Text = GetAttachName(int.Parse(couOutLinModel.COutLine_Attach.ToString()));
                }
            }
            catch
            {
                ERRLbl.Text = "系统异常，参数不合法！";
                ERRLbl.Visible = true;
                ModifyBtn.Visible = false;
            }
        }

        protected void Modify_Click(object sender, EventArgs e)
        {
            #region 验证
            if (tb_KeyWord.Text.Equals(""))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "关键字不能为空!");
                return;
            }
            if (tb_Content.Text.Equals(""))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "大纲内容不能为空!");
                return;
            }
            #endregion
            LabMS.BLL.CourseOutLine OutLine = new LabMS.BLL.CourseOutLine();
            LabMS.Model.CourseOutLine model = new LabMS.Model.CourseOutLine();
            model.COutLine_Submiter = tb_Submitter.Text;
            model.COutLine_SubmitTime = DateTime.Parse(tb_Time.Text.Trim());
            model.COutLine_KeyWord = tb_KeyWord.Text.Trim();
            model.COutLine_CourseID = Convert.ToInt32(tb_CourseID.Text.Trim());
            model.COutLine_Course = tb_CourseName.Text;
            model.COutLine_CourseCode = tb_CourseCode.Text;
            model.COutLine_Content = tb_Content.Text;

            if (IsDelCB.Checked)
            {
                int AttachIDTemp=UpLoadFile();
                if (AttachIDTemp == 0 || AttachIDTemp == -1)
                {
                    return;
                }
                else
                {
                    model.COutLine_Attach = AttachIDTemp;
                }
            }
            else
            {
                model.COutLine_Attach = int.Parse(AttachID.Value);
            }
            model.ID = int.Parse(CouOutLinID.Value);
            OutLine.Update(model);
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "<script type='text/javascript'>alert('更新成功！');window.location.href='CourseOutLinePreview.aspx"+model.ID+"';</script>");
        }
        protected int UpLoadFile()
        {
            string savePath = HttpContext.Current.Request.MapPath(@"../Content/UpLoadFile/");
            string Append = DateTime.Now.ToString("yyyyMMddhhmmss");
            string FullPathFileName = savePath.Trim() + Append.Trim() + "_" + fp_Attachment.FileName.Trim();
            int fileLength = fp_Attachment.PostedFile.ContentLength;
            if (fileLength == 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "<script type='text/javascript'>alert('文件：" + fp_Attachment.FileName + "为空文件，请重新上传！')</script>");
                return 0;
            }
            else if (fileLength > 2000 * 1024)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", "<script type='text/javascript'>alert('文件：" + fp_Attachment.FileName + "太大！,请重新上传')</script>");
                return -1;
            }
            int attachID = -2;
            bool save = false;
            if (fp_Attachment.HasFile)
            {
                try
                {
                    save = true;
                    fp_Attachment.SaveAs(FullPathFileName);

                }
                catch
                {
                    save = false;
                    throw new Exception("File Save Error");
                }
            }                      
            if (save)
            {
                LabMS.BLL.Attachment Attach = new LabMS.BLL.Attachment();
                LabMS.Model.Attachment attach = new LabMS.Model.Attachment();
                attach.Attach_Name = fp_Attachment.FileName;
                attach.Attach_Address = "Content/UpLoadFile/" + fp_Attachment.FileName + "_" + Append.Trim() + fp_Attachment.FileName.Substring(fp_Attachment.FileName.LastIndexOf("."));
                attach.Attach_Type = "1";
                attachID = Attach.Add(attach);
            }
            return attachID;
        }

        protected string GetAttachName(int ID)
        {
            LabMS.BLL.Attachment att = new LabMS.BLL.Attachment();
            LabMS.Model.Attachment attModel = new LabMS.Model.Attachment();

            attModel = att.GetModel(ID);

            return attModel.Attach_Name;
        }
    }
}
